/*
 * VistaAgencias.java
 *
 * Created on 20 de noviembre de 2008, 04:42 PM
 */

package ddta;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JOptionPane; 
import javax.swing.table.DefaultTableModel; 


/**
 *
 * @author  julio
 */

public class VistaAgencias extends javax.swing.JDialog {
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.AB68BE5D-0B32-0B2C-D0D6-FE4D67C93162]
    // </editor-fold> 
    private DDTA ddta;
    private DefaultTableModel tableModel;     
    /** Creates new form VistaAgencias */
    public VistaAgencias(java.awt.Frame parent, boolean modal ,DDTA ddta) {
        super(parent, modal);
        initComponents();
        this.ddta = ddta; 
        tblAgencias.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        tableModel = (DefaultTableModel) tblAgencias.getModel();
        mostrarGrillaAgencias();     
    }
        
    public void mostrarGrillaAgencias(){
        // llena en la grilla
        tableModel.setRowCount(ddta.getCtlAgencias().getAgencias().size());
        for(int i=0; i<ddta.getCtlAgencias().getAgencias().size(); i++){ 
            Agencia agencia = ddta.getCtlAgencias().getAgencias().get(i); 
            tableModel.setValueAt(agencia.getCodigoAgencia(), i, 0); 
            tableModel.setValueAt(agencia.getNombreAgencia(), i, 1); 
            tableModel.setValueAt(agencia.getNombreResponsable(), i, 2);  
            tableModel.setValueAt(this.ddta.obtenerNumVentasRealizadas(agencia), i, 3);
        }             
    }
    private void mostrarAgencias(){
       int filaSeleccionada = tblAgencias.getSelectedRow(); 
       if (filaSeleccionada != -1){ 
           txtcodigoAgencia.setText(tableModel.getValueAt(filaSeleccionada, 0).toString()); 
           txtnombreAgencia.setText(tableModel.getValueAt(filaSeleccionada, 1).toString()); 
           txtEncargado.setText(tableModel.getValueAt(filaSeleccionada, 2).toString()); 
       }                   
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtcodigoAgencia = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtnombreAgencia = new javax.swing.JTextField();
        txtEncargado = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnGrabar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAgencias = new javax.swing.JTable();
        btnEliminar = new javax.swing.JButton();
        btnTours = new javax.swing.JButton();
        btnVentaPaquete = new javax.swing.JButton();
        btnRegGuias = new javax.swing.JButton();
        btnPaquetesCons = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agencias DDTA");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nueva agencia"));

        jLabel1.setText("Codigo:");

        txtcodigoAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcodigoAgenciaActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre Agencia:");

        txtnombreAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnombreAgenciaActionPerformed(evt);
            }
        });

        txtEncargado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEncargadoActionPerformed(evt);
            }
        });

        jLabel3.setText("Encargado");

        btnGrabar.setText("Guardar agencia");
        btnGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGrabarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnGrabar)
                    .addComponent(txtcodigoAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtnombreAgencia, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .addComponent(txtEncargado, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtcodigoAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtnombreAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtEncargado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGrabar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de agencias"));

        tblAgencias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Responsable", "# ventas"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAgencias.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAgenciasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblAgencias);
        tblAgencias.getColumnModel().getColumn(0).setPreferredWidth(50);
        tblAgencias.getColumnModel().getColumn(1).setPreferredWidth(150);
        tblAgencias.getColumnModel().getColumn(2).setPreferredWidth(150);

        btnEliminar.setText("Eliminar agencia");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnTours.setText("Editar Tours turísticos");
        btnTours.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToursActionPerformed(evt);
            }
        });

        btnVentaPaquete.setText("Reserva de paquetes");
        btnVentaPaquete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVentaPaqueteActionPerformed(evt);
            }
        });

        btnRegGuias.setText("Reservar guias");
        btnRegGuias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegGuiasActionPerformed(evt);
            }
        });

        btnPaquetesCons.setText("Consultar Paquetes reservados");
        btnPaquetesCons.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPaquetesConsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnTours)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnVentaPaquete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRegGuias)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPaquetesCons)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminar)
                    .addComponent(btnTours)
                    .addComponent(btnVentaPaquete)
                    .addComponent(btnRegGuias)
                    .addComponent(btnPaquetesCons))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnSalir.setText("   Salir   ");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtcodigoAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcodigoAgenciaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtcodigoAgenciaActionPerformed

private void txtnombreAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnombreAgenciaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtnombreAgenciaActionPerformed

private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
    String codigoAgencia = txtcodigoAgencia.getText(); 
    String nombreAgencia = txtnombreAgencia.getText(); 
    String encargado = txtEncargado.getText().toString(); 
    if (! codigoAgencia.equals("") && ! nombreAgencia.equals("")){ 
      if (this.ddta.getCtlAgencias().buscarAgencia(codigoAgencia) == null){ 
          this.ddta.getCtlAgencias().registrarAgencia(codigoAgencia, nombreAgencia, encargado); 
          mostrarGrillaAgencias(); 
          txtcodigoAgencia.setText(""); 
          txtnombreAgencia.setText(""); 
          txtEncargado.setText(""); 
          txtcodigoAgencia.requestFocusInWindow();
          this.ddta.getCtlAgencias().informarDDTA();
      } 
      else { 
          JOptionPane.showMessageDialog(rootPane, "Código ya existe");       
      } 
    } 
}//GEN-LAST:event_btnGrabarActionPerformed

private void txtEncargadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEncargadoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtEncargadoActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
  int filaSeleccionada = tblAgencias.getSelectedRow(); 
  if (filaSeleccionada != -1){ 
      String codigoAgencia = tableModel.getValueAt(filaSeleccionada, 0).toString(); 
      ddta.getCtlAgencias().eliminarAgencia(codigoAgencia);
      mostrarGrillaAgencias();
  } else {
      JOptionPane.showMessageDialog(this, "Seleccione una agencia");
  }
}//GEN-LAST:event_btnEliminarActionPerformed

private void tblAgenciasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAgenciasMouseClicked
    //this.mostrarAgencias();
}//GEN-LAST:event_tblAgenciasMouseClicked

private void btnToursActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToursActionPerformed
// TODO add your handling code here:
    int iRow = tblAgencias.getSelectedRow();
    if (iRow != -1) {
        String codAgencia = tableModel.getValueAt(iRow, 0).toString();
        VistaPaquetes frmpaquetes = new VistaPaquetes(this, true, this.ddta, codAgencia);
        frmpaquetes.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(this, "Seleccione una agencia");
    }
}//GEN-LAST:event_btnToursActionPerformed

private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
}//GEN-LAST:event_btnSalirActionPerformed

private void btnRegGuiasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegGuiasActionPerformed
// TODO add your handling code here:
    int iRow = tblAgencias.getSelectedRow();
    if (iRow != -1) {
        String codAgencia = tableModel.getValueAt(iRow, 0).toString();
        VistaReservaGuias frmResev = new VistaReservaGuias(this, true, this.ddta, codAgencia);
        frmResev.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(this, "Seleccione una agencia");
    }
}//GEN-LAST:event_btnRegGuiasActionPerformed

private void btnVentaPaqueteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVentaPaqueteActionPerformed
// TODO add your handling code here:
    int iRow = tblAgencias.getSelectedRow();
    if (iRow != -1) {
        String codAgencia = tableModel.getValueAt(iRow, 0).toString();
        VistaReservasPaquetes frmPaq = new VistaReservasPaquetes(this, true, this.ddta, codAgencia);
        frmPaq.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(this, "Seleccione una agencia");
    }
}//GEN-LAST:event_btnVentaPaqueteActionPerformed

private void btnPaquetesConsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPaquetesConsActionPerformed
// TODO add your handling code here:
    VistaReservaPaquetesConsulta fPaq = new VistaReservaPaquetesConsulta(null, true, this.ddta);
    fPaq.setVisible(true);
}//GEN-LAST:event_btnPaquetesConsActionPerformed

    /**
     *  Creates new form VistaAgencias
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.4AB76477-7AB6-96DF-7999-13F204D8F1CB]
    // </editor-fold> 
    public DDTA getDDTA () {
        return ddta;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.5262C92F-8D74-6BE6-ADBE-A4D508A00C91]
    // </editor-fold> 
    public void setDDTA (DDTA val) {
        this.ddta = val;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGrabar;
    private javax.swing.JButton btnPaquetesCons;
    private javax.swing.JButton btnRegGuias;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnTours;
    private javax.swing.JButton btnVentaPaquete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAgencias;
    private javax.swing.JTextField txtEncargado;
    private javax.swing.JTextField txtcodigoAgencia;
    private javax.swing.JTextField txtnombreAgencia;
    // End of variables declaration//GEN-END:variables

}
